<?php
namespace app\store\validate;

use think\facade\Db;
use think\Validate;

class Goods extends Validate
{

    // 验证规则
    protected $rule = [
        'goods_name' => 'require|min:2|max:150|unique:goods',
        //'goods_sn' => 'unique:goods|max:50',
        'cat_id_2' => 'require',
        'market_price' => 'require|float',
        'goods_price' => 'require|float|lt:market_price',
        'cost_price' => 'require|float|elt:goods_price|gt:0',
        'ship_price' => 'require|float',
        'award' => 'require',
        'goods_img' => 'require',
        'store_count' => 'require|number',
        'sort' => 'require|number',
        'goods_content' => 'require',
    ];
    //错误信息
    protected $message = [
        'goods_name.require' => '商品名称必填',
        'goods_name.min' => '名称长度至少2个字符',
        'goods_name.max' => '名称长度至多50个汉字',
        'goods_name.unique' => '商品名称重复',
        'cat_id_2.require' => '请选择二级分类',
        //'goods_sn.unique' => '商品货号重复',
        //'goods_sn.max' => '商品货号至多50个字符',
        'market_price.require' => '市场价格必填',
        'market_price.float' => '市场价格只能为数字',
        'goods_price.require' => '商品价格必填',
        'goods_price.float' => '商品价格只能为数字',
        'goods_price.lt' => '商品价格必须小于市场价格',
        'cost_price.require' => '请输入成本价格',
        'cost_price.float' => '成本价格只能为数字',
        'cost_price.elt' => '成本价格必须小于等于商品价格',
        'cost_price.gt' => '成本价格必须大于0',
        'ship_price.require' => '请输入运费价格,不需要填写0!',
        'ship_price.float' => '运费价格只能为数字',
        'award.require' => '请输入奖励,不需要填写0!',
        'goods_img.require' => '商品主图必须上传',
        'store_count.require' => '商品库存必填',
        'store_count.number' => '商品库存只能为数字',
        'sort.require' => '商品排序必填',
        'sort.number' => '商品排序只能为数字',
        'goods_content.require' => '商品详情必填',
    ];
    function check_require($value, $data){
        if ($data['goods_type']!=3){
            return true;
        }
        return false;
    }
    function check_seckill_price($value, $rule, $data){
        if ($data['is_seckill']==0){
            return true;
        }
        if ($value>$data['goods_price']){
            return '秒杀价格必须小于等于商品价格';
        }
        if ($value<$data['cost_price']){
            return '秒杀价格必须大于等于成本价格';
        }
        return true;
    }
}
